home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SRC1229A.ARJ / AUDIT.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  3KB  |  126 lines

  1. /* Routines for auditing mbuf consistency. Not used for some time, may
  2.  * not be up to date.
  3.  * Copyright 1991 Phil Karn, KA9Q
  4.  */
  5. #include <stdio.h>
  6. #include "global.h"
  7. #include "mbuf.h"
  8.  
  9. extern char _Uend;
  10. extern int _STKRED;
  11.  
  12. union header {
  13.     struct {
  14.         union header *ptr;
  15.         unsigned size;
  16.     } s;
  17.     long l;
  18. };
  19.  
  20. void audit __ARGS((struct mbuf *bp,char *file,int line));
  21. static void audit_mbuf __ARGS((struct mbuf *bp,char *file,int line));
  22. static void dumpbuf __ARGS((struct mbuf *bp));
  23.  
  24. /* Perform sanity checks on mbuf. Print any errors, return 0 if none,
  25.  * nonzero otherwise
  26.  */
  27. void
  28. audit(bp,file,line)
  29. struct mbuf *bp;
  30. char *file;
  31. int line;
  32. {
  33.     register struct mbuf *bp1;
  34.  
  35.     for(bp1 = bp;bp1 != NULLBUF; bp1 = bp1->next)
  36.         audit_mbuf(bp1,file,line);
  37. }
  38.  
  39. static void
  40. audit_mbuf(bp,file,line)
  41. register struct mbuf *bp;
  42. char *file;
  43. int line;
  44. {
  45.     union header *blk;
  46.     char *bufstart,*bufend;
  47.     int16 overhead = sizeof(union header) + sizeof(struct mbuf);
  48.     int16 datasize;
  49.     int errors = 0;
  50.     char *heapbot,*heaptop;
  51.  
  52.     if(bp == NULLBUF)
  53.         return;
  54.  
  55.     heapbot = &_Uend;
  56.     heaptop = (char *) -_STKRED;
  57.  
  58.     /* Does buffer appear to be a valid malloc'ed block? */
  59.     blk = ((union header *)bp) - 1;
  60.     if(blk->s.ptr != blk){
  61.         printf("Garbage bp %lx\n",(long)bp);
  62.         errors++;
  63.     }
  64.     if((datasize = blk->s.size*sizeof(union header) - overhead) != 0){
  65.         /* mbuf has data area associated with it, verify that
  66.          * pointers are within it
  67.          */
  68.         bufstart = (char *)(bp + 1);
  69.         bufend = (char *)bufstart + datasize;
  70.         if(bp->data < bufstart){
  71.             printf("Data pointer before buffer\n");
  72.             errors++;
  73.         }
  74.         if(bp->data + bp->cnt > bufend){
  75.             printf("Data pointer + count past bounds\n");
  76.             errors++;
  77.         }
  78.     } else {
  79.         /* Dup'ed mbuf, at least check that pointers are within
  80.          * heap area
  81.         */
  82.  
  83.         if(bp->data < heapbot
  84.          || bp->data + bp->cnt > heaptop){
  85.             printf("Data outside heap\n");
  86.             errors++;
  87.         }
  88.     }
  89.     /* Now check link list pointers */
  90.     if(bp->next != NULLBUF && ((bp->next < (struct mbuf *)heapbot)
  91.          || bp->next > (struct mbuf *)heaptop)){
  92.             printf("next pointer out of limits\n");
  93.             errors++;
  94.     }
  95.     if(bp->anext != NULLBUF && ((bp->anext < (struct mbuf *)heapbot)
  96.          || bp->anext > (struct mbuf *)heaptop)){
  97.             printf("anext pointer out of limits\n");
  98.             errors++;
  99.     }
  100.     if(errors != 0){
  101.         dumpbuf(bp);
  102.         printf("PANIC: buffer audit failure in %s line %d\n",file,line);
  103.         fflush(stdout);
  104.         for(;;)
  105.             ;
  106.     }
  107.     return;
  108. }
  109.  
  110. static void
  111. dumpbuf(bp)
  112. struct mbuf *bp;
  113. {
  114.     union header *blk;
  115.     if(bp == NULLBUF){
  116.         printf("NULL BUFFER\n");
  117.         return;
  118.     }
  119.     blk = ((union header *)bp) - 1;
  120.     printf("bp %lx tot siz %u data %lx cnt %u next %lx anext %lx\n",
  121.         (long)bp,blk->s.size * sizeof(union header),
  122.         (long)bp->data,bp->cnt,
  123.         (long)bp->next,(long)bp->anext);
  124. }
  125.  
  126.